package com.hzhc.controller.yworder;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.CntrInfo;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.CarInfoService;
import com.hzhc.service.CntrInfoService;
import com.hzhc.service.YwOrderCntrService;
import com.hzhc.service.YwOrderService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.CarInfoVo;
import com.hzhc.vo.YwOrderCntrVo;
import com.hzhc.vo.YwOrderSgjVo;
import com.hzhc.vo.YwOrderVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author dongcy
 * @date 2024/6/20 9:14
 * @description YwOrderSgjController
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ywOrderSgj")
public class YwOrderSgjController extends BaseController {

    private final YwOrderService ywOrderService;

    private final CntrInfoService infoService;

    private final CarInfoService carInfoService;
    private final YwOrderCntrService ywOrderCntrService;

    /**
     * 散改集审核列表 list  (同散改集计划列表)
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getYwOrderSgjList")
    public ResultUtil<List<YwOrderVo>> getYwOrderSgjList(HttpServletRequest request, @RequestBody YwOrderVo vo){
        Page<YwOrderVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<YwOrderVo> list = ywOrderService.getYwOrderSgjList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 散改集 审核功能
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/sgjAudit")
    public ResultUtil<T> sgjAudit(HttpServletRequest request,@RequestBody YwOrderVo vo) {
        UserDto user = TokenUtil.getUser(request);
        vo.setUserId(user.getId());
        vo.setUserName(user.getRealname());
        return ywOrderService.updateCheck(vo);
    }


    /**
     * 散改集计划 获取车辆列表
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getSgjYwCarList")
    public ResultUtil<List<CarInfoVo>> getSgjYwCarList(HttpServletRequest request,@RequestBody CarInfoVo vo){
        List<CarInfoVo> list = carInfoService.getSgjYwCarList(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 散改集计划 获取箱列表
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getSgjYwCntrList")
    public ResultUtil<List<CntrInfo>> getSgjYwCntrList(HttpServletRequest request, @RequestBody CarInfoVo vo){
        List<CntrInfo> list = infoService.getSgjYwCntrList(vo);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 散改集计划 保存add（暂时不用）
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/AddSgjCntrAndCarMsg")
    public ResultUtil<T> AddSgjCntrAndCarMsg(HttpServletRequest request, @RequestBody YwOrderVo vo){
        Long userID = TokenUtil.getUserID(request);
        vo.setUserId(userID);
        return ywOrderService.AddSgjCntrAndCarMsg(vo);
    }

    /**
     * 散改集计划 修改(新增)
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/updateSgjPlan")
    public ResultUtil<T> updateSgjPlan(HttpServletRequest request,@RequestBody YwOrderVo vo){
        Long userID = TokenUtil.getUserID(request);
        vo.setUserId(userID);
        return ywOrderService.updateSgjPlan(vo);
    }

    /**
     * 散改集计划 下发
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/SgjIssued")
    public ResultUtil<T> SgjIssued(HttpServletRequest request,@RequestBody YwOrderVo vo){
        return ywOrderService.SgjIssued(vo);
    }

    private final static String EXPORT_EXCEL_PATH = "static/download/散改集装车计划模板.xlsx";
    private final static String EXPORT_EXCEL_HZ_PATH = "static/download/散改集回重模板.xlsx";


    /**
     * 散改集回重模板下载
     */
    @GetMapping("/exportSgjHzFile")
    public void exportSgjHzFile(HttpServletRequest request, HttpServletResponse response) {
        ywOrderCntrService.exportSgjHzFile(request, response, EXPORT_EXCEL_HZ_PATH);
    }

    /**
     * 清单导出模板下载
     */
    @GetMapping("/exportSgjFile")
    public void exportSgjFile(HttpServletRequest request, HttpServletResponse response) {
        ywOrderCntrService.exportSgjFile(request, response, EXPORT_EXCEL_PATH);
    }

    /**
     * 导入清单
     */
    @RequestMapping("/sgjCntrUpload/{orderNo}")
    public ResultUtil<List<YwOrderCntrVo>> sgjCntrUpload(@PathVariable("orderNo")String orderNo , MultipartFile file, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        return ywOrderCntrService.sgjCntrUpload(orderNo,file);
    }

    /**
     * 保存上传的箱子
     */
    @PostMapping("/importSgjCntrList")
    public ResultUtil<T> importSgjCntrList(HttpServletRequest request, @RequestBody YwOrderVo vo) {
        return ywOrderCntrService.importSgjCntrList(request,vo);
    }

    /**
     * 回重导入清单
     */
    @RequestMapping("/sgjHzCntrUpload/{orderNo}")
    public ResultUtil<List<YwOrderSgjVo>> sgjHzCntrUpload(@PathVariable("orderNo")String orderNo , MultipartFile file, HttpServletRequest request) {
        UserDto user = TokenUtil.getUser(request);
        return ywOrderCntrService.sgjHzCntrUpload(orderNo,file);
    }

    /**
     * 回重保存上传的箱子船名
     */
    @PostMapping("/importSgjCntrShipList")
    public ResultUtil<T> importSgjCntrShipList(HttpServletRequest request, @RequestBody YwOrderVo vo) {
        return ywOrderCntrService.importSgjCntrShipList(request,vo.getId() , vo.getCntrList());
    }

    /**
     * 散改集 完工功能
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/sgjFinish")
    public ResultUtil<T> sgjFinish(HttpServletRequest request,@RequestBody YwOrderVo vo) {
        UserDto user = TokenUtil.getUser(request);
        vo.setUserId(user.getId());
        vo.setUserName(user.getRealname());
        return ywOrderService.updateFinish(vo);
    }
}
